﻿using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MySqlQueryToolkit;
using MySqlQueryToolkit.DataAccess;

namespace MySqlQueryToolkitTests
{
    [TestClass]
    public class ExplainQueryRepositoryTests
    {
        private IDataAccess _dataAccess;

        [TestInitialize]
        public void TestInitialize()
        {
            _dataAccess = new MySqlDataAccess("Server=localhost;Uid=root;Pwd=root;port=3306;");
            _dataAccess.CommandText =
                @"CREATE TABLE IF NOT EXISTS `test`.`tblTest` (
                  `Id` INT NOT NULL AUTO_INCREMENT ,
                  `Name` VARCHAR(45) NULL ,
                  PRIMARY KEY (`Id`) );
                ";
            _dataAccess.ExecuteNonQuery();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            _dataAccess.CommandText = "DROP TABLE IF EXISTS `test`.`tblTest`;";
            _dataAccess.ExecuteNonQuery();
        }

        [TestMethod]
        [TestCategory("Integration")]
        public void GetExplainQueryExpectCorrectObject()
        {
            IExplainQueryRepository target = new ExplainQueryRepository(_dataAccess);

            var results = target.GetEntitiesFromDatabase("EXPLAIN SELECT * FROM test.tblTest;");

            var result = results.SingleOrDefault();
            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Id);
            Assert.AreEqual("SIMPLE", result.SelectType);
            Assert.AreEqual("tblTest", result.Table);
            Assert.AreEqual("ALL", result.Type);
            Assert.AreEqual(string.Empty, result.PossibleKeys);
            Assert.AreEqual(string.Empty, result.Key);
            Assert.AreEqual(0, result.KeyLength);
            Assert.AreEqual(string.Empty, result.KeyReferencedBy);
            Assert.AreEqual(1, result.Rows);
            Assert.AreEqual(string.Empty, result.Extra);
        }
    }
}
